<?php
/**
 * 减脂后台相关接口
 */

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use App\Services\IdEncryptService;
use App\Models\LogHelper;
use UserRecordService;
use TestingDetailService;
class JzApiController extends Controller
{

    /**减脂后台引流工具根据营养师和测评id获取该营养师的引流用户列表
     * $a = array(
     *  'page'=>1,
     *  'tid'=>[21,67],
     *  'nutritionist_id'=>21,
     * );
     * base64_encode(json_encode($a));
     * @param Request $request
     * @return array
     */
    public function getRecord(Request $request){
        $param = $request->input('param');
        $param = json_decode(base64_decode($param),true);
        $tid = $param['tid'];
        $nutritionist_id = $param['nutritionist_id'];
        $page = $param['page'];
        //bool $all   true代表全部，false代表激活的
        $all = $param['all'];
        if($tid && $nutritionist_id){
            $data = UserRecordService::getRrcordByNutritionist($tid ,$nutritionist_id,$page,20, $all);
        }else{
            $data = null;
        }
        return apiSuccess($data);
    }

    /**订单id更新
     * $a = array(
     *  'id'=>301,
     *  'order_id'=>4,
     * );
     * base64_encode(json_encode($a));
     * @param Request $request
     * @return array
     */
    public function updateRecord(Request $request){
        $param = $request->input('param');
        $param = json_decode(base64_decode($param),true);
        LogHelper::log('flowTesting_updateRecord====='.print_r($param,true));
        $id = $param['id'];
        $order_id = $param['order_id'];
        if($id){
            $data = UserRecordService::updateOrder($id,$order_id);
        }else{
            $data = 0;
        }
        return apiSuccess($data);
    }

    /**邀请码逻辑测评记录更新
     * @param Request $request
     * @return array
     */
    public function invitation(Request $request){
        $where = $request->input('where');
        $update = $request->input('update');
        LogHelper::log('invitationTesting where=====>'.$where);
        LogHelper::log('invitationTesting update=====>'.$update);
        $where = json_decode(base64_decode($where),true);
        $update = json_decode(base64_decode($update),true);
        $result = UserRecordService::updateInvitation($where,$update);
        return apiSuccess($result);
    }

    /**获取题目中自动填充的部分
     * @param Request $request
     * @return array
     */
    public function getJzMetaInfo(Request $request){
        $free_order_id = $request->input('id');
        $tid = $request->input('tid');
        $tid = IdEncryptService::encryption($tid,'decode');
        $data = [];
        if($tid){
            $tid = $tid[0];
            $data = TestingDetailService::getItemResult($tid,$free_order_id);
        }
        return  apiSuccess($data);
    }

}
